package com.cat.mrli.rat.programs;

import android.content.Context;
import android.opengl.GLES20;

import com.cat.mrli.rat.R;

/**
 * Created by mrli on 2017/6/25.
 */

public class TextureShaderProgram extends ShaderProgram {

    private final int uMatrixLocation;
    private final int uTextureUnitLocationTable;
    private final int uTextureUnitLocationIcon;

    private final int aPositionLocation;
    private final int aTextureCoordinatesLocation;


    public TextureShaderProgram(Context context) {
        super(context, R.raw.texture_vertex_shader, R.raw.texture_fragment_shader);

        uMatrixLocation = GLES20.glGetUniformLocation(program, U_MATRIX);
        uTextureUnitLocationTable = GLES20.glGetUniformLocation(program, U_TEXTURE_UNIT_TABLE);
        uTextureUnitLocationIcon= GLES20.glGetUniformLocation(program, U_TEXTURE_UNIT_ICON);

        aPositionLocation = GLES20.glGetAttribLocation(program, A_POSITION);
        aTextureCoordinatesLocation = GLES20.glGetAttribLocation(program, A_TEXTURE_COORDINATES);
    }


    public void setUniforms(float[] matrix, int textureId, int textureIDs) {

        GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
        GLES20.glUniform1i(uTextureUnitLocationTable, 0);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIDs);
        GLES20.glUniform1i(uTextureUnitLocationIcon, 0);
    }


    public int getPositionAttributeLocation() {
        return aPositionLocation;
    }

    public int getTextureCoordinatesAttributeLocation() {
        return aTextureCoordinatesLocation;
    }
}
